//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CommunityServer.Components;
using CommunityServer.Controls;
using CommunityServer.Discussions.Components;

namespace CommunityServer.Discussions.Controls 
{
    // *********************************************************************
    //  ThreadJoin
    //
    /// <summary>
    /// This sever control is used to join two threads into a single thread.
    /// </summary>
    /// 
    // ********************************************************************/ 
    public class ThreadJoin : TemplatedWebControl
    {
        #region Child Controls

        HyperLink childThread;
        HyperLink parentThread;
        IButton validateParent;
        CheckBox sendEmail;
        TextBox parentThreadID;
        LinkButton cancelMove;
        LinkButton move;
        CheckBox parentIsValid;

        CSContext csContext;
        ForumPost postToJoin;

        #endregion

        protected override void OnInit(EventArgs e) 
        {
            csContext = CSContext.Current;

            if (SkinName == null)                
                ExternalSkinFileName = "Moderation/View-ThreadJoin.ascx";
            else 
                ExternalSkinFileName = SkinName;
			
            if (csContext.ReturnUrl == null) 
            {
                throw new CSException(CSExceptionType.ReturnURLRequired);
            }

            // Get the post we want to move
            //
            postToJoin = Posts.GetPost(csContext.PostID, csContext.User.UserID);

            // Do an access check
            //
            Permissions.AccessCheck( postToJoin.Section, Permission.Moderate, csContext.User, postToJoin );

            base.OnInit(e);
        }
		
        protected override void OnLoad(EventArgs e) 
        {
            if (!Page.IsPostBack)
                DataBind();

            base.OnLoad(e);
        }

        #region DataBind

        public override void DataBind() 
        {
            base.DataBind();            

            // Display child thread
            //
            if (null != childThread)
            {
                childThread.Text = postToJoin.Subject + " (" + postToJoin.ThreadID + ")";
                childThread.NavigateUrl = Globals.GetSiteUrls().Post(postToJoin.PostID);
            }

            // Validate parent
            //
            if (null != validateParent)
                validateParent.Text = ResourceManager.GetString("ThreadJoin_ValidateButton");

            if (parentIsValid != null)
            {
                parentIsValid.Text = ResourceManager.GetString("ThreadJoin_IsValid");
                parentIsValid.Enabled = false;
            }

            // Cancel
            //
            if (null != cancelMove) 
            {
                cancelMove.Text = ResourceManager.GetString("MovePost_CancelMove");
            }

            // Join threads
            //
            if (null != move) 
            {
                move.Text = ResourceManager.GetString("ThreadJoin_JoinThread");
                move.Enabled = false;
            }

            // Send email
            //
            if (sendEmail != null)
                sendEmail.Text = ResourceManager.GetString("ThreadJoin_SendUserEmail");
        }
		
        #endregion

        #region Skin
                
        protected override void AttachChildControls() 
        {
            // Bind controls
            //
            parentThreadID = (TextBox) FindControl("ParentThreadID");
            childThread = (HyperLink) FindControl("ChildThread");
            parentThread = (HyperLink) FindControl("ParentThread");
            validateParent = FindButton("ValidateParentThread");            
            parentIsValid = (CheckBox) FindControl("ParentThreadIsValid");
            cancelMove = (LinkButton) FindControl("CancelMove");
            move = (LinkButton) FindControl("MovePost");
            sendEmail = (CheckBox) FindControl("SendUserEmail");
			
            InitializeChildControls();
        }

        private void InitializeChildControls() 
        {
            if (validateParent != null)
                validateParent.Click += new EventHandler(ValidateThread_Click);
		
			if (null != cancelMove)
				cancelMove.Click += new EventHandler(CancelMove_Click);

            if (null != move) 
                move.Click += new System.EventHandler(SplitThread_Click);
        }
		        
        #endregion

        #region Event Handlers

        // *********************************************************************
        //  SplitThread_Click
        //
        /// <summary>
        /// Event handler for splitting a thread
        /// </summary>
        /// 
        // ***********************************************************************/
        private void SplitThread_Click(Object sender, EventArgs e) 
        {
            // Are we valid?
            Moderate.ThreadJoin( Posts.GetPost(int.Parse(parentThreadID.Text), csContext.User.UserID), postToJoin, csContext.User.UserID, sendEmail.Checked);

			Redirect();
        }

		private void CancelMove_Click(Object sender, EventArgs e)
		{
			Redirect();
		}

		private void Redirect()
		{
			// Redirect the user to the return url.
			HttpContext.Current.Response.Redirect(csContext.ReturnUrl);
			HttpContext.Current.Response.End();
		}

        private void ValidateThread_Click(Object sender, EventArgs e) 
        {
            ForumPost p = null;

            // Check to ensure that a post exists
            try 
            {
                p = Posts.GetPost(int.Parse(parentThreadID.Text), csContext.User.UserID);
            } 
            catch 
            {
                return;
            }

            if (p == null)
                return;

            parentThreadID.Text = p.PostID.ToString();
            parentIsValid.Checked = true;
            parentThread.Text = p.Subject + " (" + p.ThreadID + ")";
            parentThread.NavigateUrl = Globals.GetSiteUrls().Post(p.PostID);

            move.Enabled = true;
        }

        #endregion
    }
}
